package cn.com.huzo.webapp.controller.hr;

import cn.com.huzo.model.entities.hr.LeaveApplicationEntity;
import cn.com.huzo.service.hr.LeaveApplicationManager;
import cn.com.huzo.service.master.LeaveApplicationTypeManager;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import huzofw.common.web.controller.BaseFormController;
import java.util.Map;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.collections4.MapUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

/**
 * 请假单
 *
 * @author 吴旭
 */
@Controller
@RequestMapping(value = "/hr/leave_application")
public class LeaveApplicationController extends BaseFormController {

    // <editor-fold defaultstate="collapsed" desc="属性">
    @Resource(name = "LeaveApplicationManager")
    private LeaveApplicationManager _manager;

    @Resource(name = "LeaveApplicationTypeManager")
    private LeaveApplicationTypeManager _manager_leave_application_type;
    // </editor-fold>

    // <editor-fold defaultstate="collapsed" desc="画面处理部分">
    /**
     * 跳转到请假单一览画面
     * <p>
     * @param request
     * @return
     * @throws java.lang.Exception
     */
    @RequestMapping("/list.html")
    public ModelAndView redirectListWnd(HttpServletRequest request) throws Exception {
        ModelAndView view = new ModelAndView();
        view.setViewName("hr.leave_application.list.def");
        return view;
    }

    /**
     * 跳转到请假单编辑画面
     *
     * @return
     * @throws java.lang.Exception
     */
    @RequestMapping("/edit.html")
    public ModelAndView redirectEditPopupWnd() throws Exception {
        ModelAndView view = new ModelAndView();
        // 为画面设置变量
        view.addObject("LeaveApplicationTypeList", _manager_leave_application_type.fetchAll());  // 设置请假类型
        view.setViewName("popup.hr.leave_application.edit.def");
        return view;
    }

    /**
     * 跳转到请假单编辑画面
     *
     * @return
     * @throws java.lang.Exception
     */
    @RequestMapping("/approve.html")
    public ModelAndView redirectApprovePopupWnd() throws Exception {
        ModelAndView view = new ModelAndView();
        // 为画面设置变量
        view.addObject("LeaveApplicationTypeList", _manager_leave_application_type.fetchAll());  // 设置请假类型
        view.setViewName("popup.hr.leave_application.approve.def");
        return view;
    }
    // </editor-fold>

    // <editor-fold defaultstate="collapsed" desc="数据处理部分">
    /**
     * 根据ID取得发货发令单数据。
     *
     * @param criterion 发货发令单标识、当前用户、流程标识
     * @return 发货发令单数据。
     * @throws java.lang.Exception
     */
    @RequestMapping(value = "/fetchByKey.html",
            method = RequestMethod.POST)
    public @ResponseBody
    Map<String, Object> fetchByKey(@RequestBody final Map<String, Object> criterion) throws Exception {
        return _manager.fetchByHeaderKey(MapUtils.getString(criterion, "LeaveApplicationId"));
    }

    /**
     * 根据条件取得请假申信息分页数据。
     * <p>
     * @param criterion 检索条件
     * @return 分页数据
     * @throws Exception
     */
    @RequestMapping(value = "/fetchForPage.html",
            method = RequestMethod.POST)
    public @ResponseBody
    Map<String, Object> fetchForPage(@RequestBody Map<String, Object> criterion) throws Exception {
        return _manager.fetchForPage(criterion);
    }

    /**
     * 创建请假单并启动请假申请流程。
     * <p>
     * @param criterion 请假单、当前页、每页显示数、相关条件。
     * @return 请假流程
     * @throws Exception
     */
    @RequestMapping(value = "/start.html",
            method = RequestMethod.POST)
    public @ResponseBody
    Map<String, Object> startLeave(@RequestBody final Map<String, Object> criterion) throws Exception {
        ObjectMapper mapper = new ObjectMapper();
        LeaveApplicationEntity instance = mapper.convertValue(MapUtils.getObject(criterion, "LeaveApplicationEntity"), new TypeReference<LeaveApplicationEntity>() {
        });

        return _manager.startWorkflow(instance,
                MapUtils.getInteger(criterion, "Limit"),
                MapUtils.getInteger(criterion, "PageIndex"));
    }

    /**
     * 办理业务（通过、驳回）。
     * <p>
     * @param criterion 审批人、任务ID
     * @return 成功标记
     * @throws Exception
     */
    @RequestMapping(value = "/handleTask.html",
            method = RequestMethod.POST)
    public @ResponseBody
    Map<String, Object> handleTask(@RequestBody Map<String, Object> criterion) throws Exception {
        return _manager.handleTask(MapUtils.getLong(criterion, "ApprovUserId"),
                MapUtils.getLong(criterion, "StartUserId"),
                MapUtils.getString(criterion, "TaskId"),
                MapUtils.getString(criterion, "ProcessInstancesId"),
                MapUtils.getBoolean(criterion, "Approve"),
                MapUtils.getString(criterion, "FlowName"),
                MapUtils.getString(criterion, "Comment"));
    }

    /**
     * 销假。
     * <p>
     * @param instance 实际开始、结束时间
     * @return 成功标记
     * @throws Exception
     */
    @RequestMapping(value = "/end.html",
            method = RequestMethod.POST)
    public @ResponseBody
    Integer endLeave(@RequestBody final LeaveApplicationEntity instance) throws Exception {
        return _manager.update(instance);
    }

    /**
     * 调整申请。
     * <p>
     * @param instance 计划开始、结束时间、请假原因等
     * @return 成功标记
     * @throws Exception
     */
    @RequestMapping(value = "/reapply.html",
            method = RequestMethod.POST)
    public @ResponseBody
    Integer reapply(@RequestBody final LeaveApplicationEntity instance) throws Exception {
        return _manager.reapply(instance);
    }
    // </editor-fold>
}
